//
//  StudentsActicitiesViewController.m
//  ECBBusinessCamp
//
//  Created by rimi on 16/6/16.
//  Copyright © 2016年 邓毕华. All rights reserved.
//

#import "StudentsActicitiesViewController.h"
 
@interface StudentsActicitiesViewController ()<UITableViewDataSource,UITableViewDelegate>


#pragma mark -- 属性 --
#pragma mark --
@property (weak, nonatomic) IBOutlet UITableView *tableview;

@property (weak, nonatomic) IBOutlet UIButton *addActivityBtn;


@property (nonatomic,strong) NSMutableArray *arrSource;
@property (nonatomic,strong) StudentActicitesDetailViewController *studentActicitesDetailVc;
@property (nonatomic,strong) StudentAddActivityViewController *studentAddActivityVc;

@property (nonatomic,assign) NSInteger pageNum;
@property (nonatomic,assign) NSInteger allpage;

@end

@implementation StudentsActicitiesViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableview.rowHeight = 380;
    [self addRefreshHeader];
    [self addRefreshFooter];
    [self loadData];
    
}



#pragma mark -- 刷新 --
#pragma mark --
- (void)addRefreshHeader {
    
    _tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
         //下拉刷新
        _pageNum = 0;
        [_arrSource removeAllObjects];
        [self loadData];//当前第一页
        [_tableview.mj_header endRefreshing];
        
 
    }];
    
}

- (void)addRefreshFooter {
    
   
    _tableview.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        // 上拉加载更多
        if (_arrSource.count >= _allpage) {
            [MBProgressHUD showError:@"没有更多数据！"];
            [_tableview.mj_footer endRefreshing];
            return ;
        }//页数不足时结束刷新
        
        _pageNum += 1;//后一页
        [self loadData];
        [self.tableview reloadData];
        [_tableview.mj_footer endRefreshing];
    }];
}
#pragma mark -- 数据请求 --
#pragma mark --

- (void)loadData{
    
     NSMutableDictionary * parameters = [NSMutableDictionary dictionaryWithObjects:@[LoginUserInfo.defaultUserInfo.userid,@(_pageNum+1),@(5)] forKeys:@[@"uid",@"pageNum",@"pageSize"]];
    
    NSLog(@"parameters==vc=%@--------",parameters);
    [[NetTool sharedTool] requestMethod:POST loadingMsg:@"加载中..." url:BASEURLAPPEND(STUDENTACTICITIES_URL) parameters:parameters finish:^(id responseObject, NSError *error) {
        [self loadDataSuccess:responseObject];
        [MBProgressHUD showSuccess:@"网络请求成功"];
        //更新视图
        [self.tableview reloadData];
        
    }];
}

#pragma mark -- 数据处理 --
#pragma mark --
- (void)loadDataSuccess:(NSDictionary *)result{
    
    NSMutableArray *temp = [NSMutableArray array];
    for (NSDictionary *dic in _arrSource) {
        [temp addObject:dic];
    }
    for (NSDictionary *dic in result[@"data"]) {
        [temp addObject:dic];
    }
    _arrSource = temp;
    _allpage = [result[@"pageCount"] integerValue];

}



#pragma mark -- UITableViewDataSource  UITableViewDelegate--
#pragma mark --
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  _arrSource.count;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    StudentsActicitiesTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"StudentsActicitiesTableViewCell"];
    if (!cell) {
        cell = [[StudentsActicitiesTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"StudentsActicitiesTableViewCell"];
    }
    
    NSString *url =  BASEURLAPPEND(_arrSource[indexPath.row][@"pic"]);
    [NSURL URLWithString:url];
    [cell.imagev sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"user_default"]];
    cell.title.text = _arrSource[indexPath.row][@"name"];
    cell.actor.text =  [NSString stringWithFormat:@"%@%@",@"发起者:",_arrSource[indexPath.row][@"teacherName"]];
    cell.starttime.text = [NSString stringWithFormat:@"%@%@",@"活动时间:",[NSDate dateWithTimeIntervalSince1970:[_arrSource[indexPath.row][@"startTime"][@"time"] doubleValue]/1000]];
    cell.endtime.text = [NSString stringWithFormat:@"%@%@",@"报名截止时间:",[NSDate dateWithTimeIntervalSince1970:[_arrSource[indexPath.row][@"catoffday"][@"time"] doubleValue]/1000]];
    cell.adress.text = [NSString stringWithFormat:@"%@%@",@"活动地点:",_arrSource[indexPath.row][@"place"]];
    cell.forecast.text = [NSString stringWithFormat:@"%@%@",@"简介:",_arrSource[indexPath.row][@"desce"]];
    
    NSInteger totalNum = [_arrSource[indexPath.row][@"totalNum"] integerValue];
    NSInteger currentNum = [_arrSource[indexPath.row][@"careentNum"] integerValue];
    cell.numberOfPerson.text = [NSString stringWithFormat:@"已有%ld人报名，剩余%ld个名额",currentNum,(totalNum - currentNum)];
    
    if ([_arrSource[indexPath.row][@"isoutdate"] isEqualToString:@"ing"]) {
        cell.isOutImage.hidden = YES;
    }
    if ([_arrSource[indexPath.row][@"isoutdate"] isEqualToString:@"out"]) {
        cell.isOutImage.hidden = NO;
    }
   
    return cell;
}




#pragma mark --详情页--
#pragma mark --

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
   [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    _studentActicitesDetailVc = [self.storyboard instantiateViewControllerWithIdentifier:@"StudentActicitesDetailViewController"];
    _studentActicitesDetailVc.detailSource = _arrSource[indexPath.row];//初始化时就付值，然后再添加视图，得到数据
    NSLog(@" _studentActicitesDetailVc.detailSource%@", _studentActicitesDetailVc.detailSource);
    _studentActicitesDetailVc.view.center = CGPointMake(self.view.center.x * 3, self.view.center.y);
    [self animationDetailvc:_studentActicitesDetailVc.view];//动画
    
    [self addChildViewController:_studentActicitesDetailVc];
    [self.view addSubview:_studentActicitesDetailVc.view];
    NSLog(@"点击%ld",(long)indexPath.row);
    
}

#pragma mark -- 详情页动画 --
#pragma mark --
- (void)animationDetailvc:(UIView *)someoneview{
    
    [UIView animateWithDuration:0.8 animations:^{
        someoneview.center = self.view.center;
    } completion:nil];
    
}
#pragma mark -- 发布按键动画 --
#pragma mark --
- (void)animationOfaddPartyView{
    
    
    [UIView animateWithDuration:0.01 animations:^{
        _studentAddActivityVc.onlyview.transform = CGAffineTransformScale(_studentAddActivityVc.onlyview.transform, 0.1, 0.1);
        _studentAddActivityVc.onlyview.hidden = YES;
    } completion:^(BOOL finished) {
        NSLog(@"发布v出现");
     
    _studentAddActivityVc.onlyview.hidden = NO;
    //动画路径
    CABasicAnimation * position = [CABasicAnimation animationWithKeyPath:@"position"];//@"position"里面是图层的属性
    //持续时间
    position.duration = 1.0f;
    //起始帧、起点~id类型~要用NSValue封装
    position.fromValue =[NSValue valueWithCGPoint:_addActivityBtn.center];
    //结束帧
    position.toValue = [NSValue valueWithCGPoint:_studentAddActivityVc.onlyview.center];
         NSLog(@"发布v扩大,btn__%@",_addActivityBtn);
    //设置重复的次数
    position.repeatCount = 1;
    //设置是否反向执行
    position.autoreverses = NO;
    //    position.autoreverses = NO;
    //设置线性规律
    position.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    
    //设置动画执行完后，将动画保留在当前值。见上文本
    position.removedOnCompletion = NO;
    position.fillMode = kCAFillModeForwards;//这里是设置动画执行完后，将动画保留在当前值。
    
    [_studentAddActivityVc.onlyview.layer addAnimation:position forKey:@"position"];//key是在移除动画时要用的值。
       
     }];
    
   
    [UIView animateWithDuration:1 animations:^{
        
        _studentAddActivityVc.onlyview.transform = CGAffineTransformScale(_studentAddActivityVc.onlyview.transform, 10, 10);
    } completion:^(BOOL finished) {
        NSLog(@"发布v扩大 ");
        
    }];
    
}

#pragma mark -- 发布活动Btn --
#pragma mark --
- (IBAction)addParty:(UIButton *)sender {
    
    NSLog(@"发布活动");
    //动画，出现发表框
    //加载一个视图控制器半透明灰色╮(╯▽╰)╭
    
    _studentAddActivityVc = [self.storyboard instantiateViewControllerWithIdentifier:@"StudentAddActivityViewController"];
   
    [self.parentViewController addChildViewController:_studentAddActivityVc];
    [self.view.superview addSubview:_studentAddActivityVc.view];
    
    //动画
    [self animationOfaddPartyView];
   
}
@end